package com.wuyou.dataStructure;

import java.util.Arrays;

/**
 * 寻找数组的中心索引
 */
public class PivotIndex {

    public static void main(String[] args) {
        PivotIndex pivotIndex = new PivotIndex();
        int[] nums = {1, 2, 3};
        System.out.println(pivotIndex.solution_1(nums));
    }

    public int solution_1(int[] nums) {
        if (nums.length <= 1) {
            return 0;
        }
        int total = Arrays.stream(nums).sum();
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            if (sum * 2 + nums[i] == total) {
                return i;
            }
            sum += nums[i];
        }
        return -1;
    }
}
